import {
  AppVersionSchemaType,
  GetLatestVersionResponse,
  ListAppBody,
  PostPublishAppProps,
  VersionListResponse,
} from '@/types/app/api';
import apiClient from '../apiClient';
import {
  FlowNodeTemplateType,
  NodeTemplateListItemType,
  ParentIdType,
  ParentTreePathItemType,
} from '@/types/workflow/node';
import {
  CreateHttpPluginBody,
  GetPreviewNodeQuery,
  GetSystemPluginTemplatesBody,
  UpdateHttpPluginBody,
} from '@/types/plugin/api';
import { PaginationProps, PaginationResponse } from '@/common';
import { PluginItem } from '@/types/plugin/type';
import { AppListItemType, AppTypeEnum } from '@/types/app/type';
import {
  defaultNodeVersion,
  FlowNodeTemplateTypeEnum,
  FlowNodeTypeEnum,
} from '@/types/workflow/constant';

export enum PluginApi {
  SystemPlugTemplates = '/api/ruigpt/main/application/plugin/fetchSystemPluginTemplates',
  SystemPluginPaths = '/api/ruigpt/main/application/plugin/path',
  PreviewPluginNode = '/api/ruigpt/main/application/plugin/fetchPreviewNode',
  CreateHttpPlugin = '/api/core/app/httpPlugin/create',
  UpdateHttpPlugin = '/api/ruigpt/main/application/httpPlugin/modify',
  ApiSchemaByUrl = '/api/ruigpt/main/application/httpPlugin/fetchApiSchemaByUrl',
  GetPluginList = '/openapi/v2/apps/plugin/list',
  GetPluginUsedCount = '/openapi/v2/apps/plugin/{id}/usedCount',
  GetTeamPluginList = '/openapi/v2/apps/team/plugin/list',
}

/**
 * 获取模板列表
 * @returns
 */
const getSystemPlugTemplates = (data: GetSystemPluginTemplatesBody) =>
  apiClient.post<NodeTemplateListItemType[]>({ url: PluginApi.SystemPlugTemplates, data });

const getSystemPluginPaths = (parentId: ParentIdType) => {
  if (!parentId) return Promise.resolve<ParentTreePathItemType[]>([]);
  return apiClient.get<ParentTreePathItemType[]>({
    url: PluginApi.SystemPluginPaths,
    params: { parentId },
  });
};

export const getPreviewPluginNode = (data: GetPreviewNodeQuery) =>
  apiClient.get<FlowNodeTemplateType>({
    url: PluginApi.PreviewPluginNode,
    params: {
      appId: data.appId,
    },
  });

const postCreateHttpPlugin = (data: CreateHttpPluginBody) =>
  apiClient.post({ url: PluginApi.CreateHttpPlugin, data });

const putUpdateHttpPlugin = (data: UpdateHttpPluginBody) =>
  apiClient.post({ url: PluginApi.UpdateHttpPlugin, data });

const getApiSchemaByUrl = (url: string) =>
  apiClient.get<AppVersionSchemaType>({
    url: PluginApi.ApiSchemaByUrl,
    params: { url },
    timeout: 30000,
  });

const getPluginList = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<PluginItem>>({ url: PluginApi.GetPluginList, params });

const getPluginUsedCount = (id: string) =>
  apiClient.get<{ count: number }>({
    url: PluginApi.GetPluginUsedCount.replace('{id}', id),
  });

const getTeamPluginList = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<AppListItemType>>({
    url: PluginApi.GetTeamPluginList,
    params,
  });

// export const getTeamPlugTemplates = () =>
//   getTeamPluginList().then((res) =>
//     res.map((app) => ({
//       tmbId: app.tmbId,
//       id: app._id,
//       pluginId: app._id,
//       isFolder:
//         app.type === AppTypeEnum.folder ||
//         app.type === AppTypeEnum.httpPlugin ||
//         app.type === AppTypeEnum.toolSet,
//       templateType: FlowNodeTemplateTypeEnum.teamApp,
//       flowNodeType:
//         app.type === AppTypeEnum.workflow
//           ? FlowNodeTypeEnum.appModule
//           : app.type === AppTypeEnum.toolSet
//           ? FlowNodeTypeEnum.toolSet
//           : FlowNodeTypeEnum.pluginModule,
//       type: app.type,
//       avatar: app.avatar,
//       name: app.name,
//       intro: app.intro,
//       showStatus: false,
//       version: app.pluginData?.nodeVersion || defaultNodeVersion,
//       isTool: true,
//     })),
//   );

export default {
  getSystemPlugTemplates,
  getSystemPluginPaths,
  getPreviewPluginNode,
  postCreateHttpPlugin,
  putUpdateHttpPlugin,
  getApiSchemaByUrl,
  getPluginList,
  getPluginUsedCount,
  getTeamPluginList,
};
